home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / tcpr-1.1.5 / pclient.pl < prev    next >
Encoding:
Text File  |  1993-05-15  |  2.5 KB  |  133 lines

  1. #
  2. # File:        pclient.pl
  3. # Author:    G. Paul Ziemba
  4. # Date:        93.01.25
  5. # SCCS:        @(#)pclient.pl    1.8 5/15/93
  6. # Purpose:    proxy ftp client for use with tcprelay
  7. #
  8.  
  9. #########################
  10. # Begin Configuration    #
  11. #########################
  12.  
  13. #$debug = 1;
  14. #$verbose = 1;
  15.  
  16. #########################
  17. # End Configuration    #
  18. #########################
  19.  
  20. $| = 1;
  21.  
  22. #
  23. # ptelnet [-v] <host> [<port>]
  24. # pftp [-v] <host> [<port>]
  25. #
  26.  
  27. $0 =~ s:^.*/::;    # basename
  28. if ($0 eq "pftp") {
  29.     $Application = $Mode = 'ftp';
  30.     $Service = 'ftp'; # unless port is specified
  31. } elsif ($0 eq "ptelnet") {
  32.     $Application = 'telnet';
  33.     $Service = 'telnet'; # unless port is specified
  34. } else {
  35.     print "$0: invalid name for myself\n";
  36.     exit 1;
  37. }
  38.  
  39. $server = shift;
  40. if ($server eq "-v") {
  41.     ++$VerboseMode;
  42.     $server = shift;
  43. }
  44. if (!defined($server)) {
  45.     print "usage: $0 [-v] <destination> [<port>]\n";
  46.     exit 1;
  47. }
  48.  
  49. if (defined($ARGV[0])) {
  50.     $Service = shift;
  51. }
  52.  
  53.  
  54.  
  55. unless (do 'sys/socket.ph') {
  56.     eval 'sub SOCK_STREAM {1;} sub AF_INET {2;} sub PF_INET {2;}';
  57. }
  58.  
  59. #
  60. # Set up relay spec (port + ipaddr)
  61. #
  62. ($name, $aliases, $port, $proto) = getservbyname("tcprpm", "tcp");
  63. ($name, $aliases, $type, $len, $gw_ip) = gethostbyname($RELAYHOST);
  64. $them = pack('S n a4 x8', &AF_INET, $port, $gw_ip);
  65.  
  66. #
  67. # connect to relay
  68. #
  69. print "Connecting to relay server $RELAYHOST ...\n" if ($VerboseMode);
  70. ($name, $aliases, $proto) = getprotobyname('tcp');
  71. socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
  72. connect(S, $them) || die "connect: $!";
  73.  
  74. select(S); $| = 1; select(stdout);
  75.  
  76.  
  77. #
  78. # Do it
  79. #
  80. print "Requesting relay ...\n" if ($VerboseMode);
  81. &transact("", '220');
  82. &transact("server $server", '250');
  83. &transact("service $Service", '250');
  84. if (defined($Mode)) {
  85.     &transact("mode $Mode", '250');
  86. }
  87. $port = &transact("relay", '212');
  88.  
  89. $port =~ tr/A-Z/a-z/;
  90. $port =~ s/^\d\d\d\s+port\s+(\d+)(\s+.*)*$/\1/;
  91.  
  92. if ($port =~ /^\d+$/)  {
  93.     $port = "-$port"
  94.         if (($Application eq "telnet") && ($CHARMODE_PORT eq "-"));
  95.     system("$Application $RELAYHOST $port");
  96. } else {
  97.     print STDERR "\nRelay protocol error\n";
  98.     exit 1;
  99. }
  100. close(S);
  101. exit 0;
  102.  
  103.  
  104. sub transact { # $send $expect
  105.     local($send, $expect) = @_[0,1];
  106.     local(@responses) = ();
  107.  
  108.     printf("send[%s] expect[%s]\n", $send, $expect) if $debug;
  109.  
  110.     print S $send."\n" unless ($send eq "");
  111.     while (<S>) {
  112.         if (/^$expect/) {
  113.             print if $verbose;
  114.             print "gotit\n" if $debug;
  115.             return $_;
  116.         }
  117.         if ($VerboseMode) {
  118.             print;
  119.         } else {
  120.             push(@responses, $_);
  121.         }
  122.         last unless (/^\d\d\d-/);
  123.     }
  124.     if (!$VerboseMode) {
  125.         for (@responses) {
  126.             print;
  127.         }
  128.     }
  129.     print "Error, closing connection\n";
  130.     close(S);
  131.     exit 1;
  132. }
  133.